package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicExpenseRecord;
import com.ruicar.afs.cloud.basic.common.service.BasicExpenseRecordService;
import com.ruicar.afs.cloud.basic.common.vo.ExpenseVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.FeeTypeEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 合同费用信息
 */
@Slf4j
@RestController
@RequestMapping("/expenseRecord")
@AllArgsConstructor
public class BasicExpenseRecordController {
    private final BasicExpenseRecordService basicExpenseRecordService;

    @ApiOperation(value = "根据合同号码查询费用列表数据")
    @PostMapping("/getExpenseRecordList")
    public IResponse<List<BasicExpenseRecord>> getPenaltyInterestList(@RequestParam("contractNo") String contractNo,
                                                                      @RequestParam(value = "costType", required = false) String costType) {
        List<BasicExpenseRecord> basicPenaltyInterests = basicExpenseRecordService.list(Wrappers.<BasicExpenseRecord>lambdaQuery()
                .eq(BasicExpenseRecord::getContractNo, contractNo)
                .eq(EmptyUtils.isNotEmpty(costType), BasicExpenseRecord::getCostType, costType));

        return IResponse.success(basicPenaltyInterests);
    }

    @ApiOperation(value = "根据合同号查询差额费用信息")
    @PostMapping("/getExpenseList")
    public IResponse getExpenseList(@RequestParam("contractNo") String contractNo) {
        List<BasicExpenseRecord> list = basicExpenseRecordService.list(Wrappers.<BasicExpenseRecord>lambdaUpdate()
                .eq(BasicExpenseRecord::getContractNo, contractNo)
                .notIn(BasicExpenseRecord::getCostType, FeeTypeEnum.penalty));
        List<ExpenseVo> expenseVoList = new ArrayList<>();
        for (BasicExpenseRecord expenseRecord : list) {
            ExpenseVo ExpenseVo = new ExpenseVo();
            ExpenseVo.setType(expenseRecord.getCostType());
            ExpenseVo.setCost(expenseRecord.getRemainingAmt());
            ExpenseVo.setSubtotal(expenseRecord.getRemainingAmt());
            expenseVoList.add(ExpenseVo);
        }
        return new IResponse().setData(expenseVoList);
    }

}
